package com.houtai.controller.job;

import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.base.Page;
import com.houtai.base.ResultVo;
import com.houtai.bean.dto.company.TdCompanyJobUpdateDto;
import com.houtai.bean.dto.job.*;
import com.houtai.bean.dto.user.job.JobUserListDto;
import com.houtai.bean.dto.user.job.UserJobInsertDto;
import com.houtai.bean.dto.user.job.UserJobResumeDto;
import com.houtai.bean.vo.company.TdCompanyJobDetailVo;
import com.houtai.bean.vo.dict.DictListResult;
import com.houtai.bean.vo.job.JobDetailVo;
import com.houtai.bean.vo.job.JobListVo;
import com.houtai.bean.vo.job.TdJobDetailVo;
import com.houtai.bean.vo.job.TdJobListVo;
import com.houtai.bean.vo.job.search.TdJobSearchListVo;
import com.houtai.bean.vo.resume.TdResumeDetailVo;
import com.houtai.bean.vo.user.job.JobUserListVo;
import com.houtai.bean.vo.user.job.UserJobListVo;
import com.houtai.service.company.CompanyService;
import com.houtai.service.dict.DictService;
import com.houtai.service.job.JobService;
import com.houtai.service.user.job.UserJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(value="岗位接口", tags={"岗位接口"})
@RequestMapping(value = {"api/v1"}, method = RequestMethod.POST)
public class JobController {
    @Autowired
    private UserJobService userJobService;
    @Autowired
    private JobService jobService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private DictService dictService;

    @ApiOperation(value="招聘处公司信息数据", notes="招聘处公司信息数据")
    @Authority(AuthorityType.CompanyReal)
    @RequestMapping(value = "/company/job/detail")
    public TdCompanyJobDetailVo company(@RequestHeader String token) {
        return companyService.tdCompanyJobDetail(token);
    }
    @ApiOperation(value="招聘处公司信息数据修改", notes="招聘处公司信息数据修改")
    @Authority(AuthorityType.CompanyReal)
    @RequestMapping(value = "/company/job/detail/update")
    public ResultVo update(@ApiParam(value = "招聘处公司信息数据修改所需参数") @Valid @RequestBody TdCompanyJobUpdateDto dto, BindingResult result,
                           @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = companyService.updateTdCompanyJob(dto, token);
        }
        return retInfo;
    }

    @Authority(value = AuthorityType.NoValidate)
    @ApiOperation(value="企业规模列表", notes="企业规模列表")
    @RequestMapping(value = "/data/company/people", method = RequestMethod.POST)
    public DictListResult people() {
        return dictService.findDictByType("company_people");
    }

    @Authority(value = AuthorityType.NoValidate)
    @ApiOperation(value="企业发展列表", notes="企业发展列表")
    @RequestMapping(value = "/data/financing/scale", method = RequestMethod.POST)
    public DictListResult scale() {
        return dictService.findDictByType("financing_scale");
    }

    @ApiOperation(value="添加岗位", notes="添加岗位")
    @Authority(AuthorityType.CompanyReal)
    @RequestMapping(value = "/job/insert")
    public ResultVo insert(@ApiParam(value = "添加岗位所需参数") @Valid @RequestBody TdJobInsertDto dto, BindingResult result,
                           @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = jobService.insertJob(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="修改岗位", notes="修改岗位")
    @Authority(AuthorityType.CompanyReal)
    @RequestMapping(value = "/job/update")
    public ResultVo update(@ApiParam(value = "修改岗位所需参数") @Valid @RequestBody TdJobUpdateDto dto, BindingResult result,
                           @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = jobService.updateJob(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="关闭岗位", notes="关闭岗位")
    @Authority(AuthorityType.CompanyReal)
    @RequestMapping(value = "/job/update/state")
    public ResultVo update(@ApiParam(value = "修改岗位所需参数") @Valid @RequestBody TdJobDeleteDto dto, BindingResult result,
                           @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = jobService.updateJobState(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="岗位详情", notes="岗位详情")
    @Authority(AuthorityType.CompanyReal)
    @RequestMapping(value = "/job/detail")
    public TdJobDetailVo detail(@ApiParam(value = "修改岗位所需参数") @Valid @RequestBody TdJobDeleteDto dto, BindingResult result,
                                @RequestHeader String token) {
        TdJobDetailVo retInfo = new TdJobDetailVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = jobService.jobDetail(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="我的岗位列表", notes="我的岗位列表")
    @Authority(AuthorityType.CompanyReal)
    @RequestMapping(value = "/job/list")
    public TdJobListVo list(@ApiParam(value = "我的岗位列表所需参数") @Valid @RequestBody Page dto, BindingResult result,
                            @RequestHeader String token) {
        TdJobListVo retInfo = new TdJobListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = jobService.jobList(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="岗位热搜列表及热门岗位列表", notes="岗位热搜列表及热门岗位列表")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/job/hot/search/list")
    public TdJobSearchListVo list() {

        return jobService.jobSearchList();
    }
    @ApiOperation(value="岗位搜索", notes="岗位搜索")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/job/search")
    public JobListVo search(@ApiParam(value = "岗位搜索所需参数") @Valid @RequestBody JobListDto dto, BindingResult result,
                          @RequestHeader String token) {
        JobListVo retInfo = new JobListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = jobService.jobSearch(dto);
        }
        return retInfo;
    }
    @ApiOperation(value="岗位搜索详情", notes="岗位搜索详情")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/job/search/detail")
    public JobDetailVo 岗位搜索详情(@ApiParam(value = "岗位搜索所需参数") @Valid @RequestBody JobDetailDto dto, BindingResult result,
                              @RequestHeader String token) {
        JobDetailVo retInfo = new JobDetailVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = jobService.jobDetailVo(dto);
        }
        return retInfo;
    }
    @ApiOperation(value="岗位申请", notes="岗位申请")
    @Authority(AuthorityType.UserReal)
    @RequestMapping(value = "/user/job/insert")
    public ResultVo insert(@ApiParam(value = "岗位申请所需参数") @Valid @RequestBody UserJobInsertDto dto, BindingResult result,
                           @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = userJobService.insertUserJob(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="我的求职记录", notes="我的求职记录")
    @Authority(AuthorityType.UserReal)
    @RequestMapping(value = "/user/job/list")
    public UserJobListVo jobList(@ApiParam(value = "岗位申请所需参数") @Valid @RequestBody Page dto, BindingResult result,
                              @RequestHeader String token) {
        UserJobListVo retInfo = new UserJobListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = userJobService.userJobList(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="岗位投递记录", notes="岗位投递记录")
    @Authority(AuthorityType.CompanyReal)
    @RequestMapping(value = "/job/user/list")
    public JobUserListVo jobUserList(@ApiParam(value = "岗位申请所需参数") @Valid @RequestBody JobUserListDto dto, BindingResult result,
                                     @RequestHeader String token) {
        JobUserListVo retInfo = new JobUserListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = userJobService.jobUserList(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="岗位投递记录查看简历", notes="岗位投递记录查看简历")
    @Authority(AuthorityType.CompanyReal)
    @RequestMapping(value = "/job/user/resume")
    public TdResumeDetailVo resumeDetail(@ApiParam(value = "岗位投递记录查看简历所需参数") @Valid @RequestBody UserJobResumeDto dto, BindingResult result,
                                        @RequestHeader String token) {
        TdResumeDetailVo retInfo = new TdResumeDetailVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = userJobService.resumeDetail(dto, token);
        }
        return retInfo;
    }
}
